package com.gxa.service.Impl;

import com.gxa.common.R;
import com.gxa.mapper.zhy.ConfigurationVariablesMapper;
import com.gxa.pojo.Params;
import com.gxa.pojo.Params_Son;
import com.gxa.pojo.zhy.JsonResult;
import com.gxa.pojo.zhy.Paging;
import com.gxa.service.ConfigurationVariablesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: 赵贺益
 * Date: 2021/12/05 16:09
 * Description:
 * Version: V1.0
 */

/**
 * 配置变量的Service
 */
@Service
public class ConfigurationVariablesServiceImpl implements ConfigurationVariablesService {

    @Autowired
    private ConfigurationVariablesMapper configurationVariablesMapper;
    /**
     * 查询所有的配置变量
     * @return
     * @param currentPage
     * @param pageSize
     */
    @Override
    public R queryAllConfigParams(Integer currentPage,Integer pageSize) {
        JsonResult jsonResult=new JsonResult();
        List<Object> resultList=new ArrayList<>();
        R r=new R();
        /**
         * 获取所有的变量条数
         */
        Integer totalsize=configurationVariablesMapper.queryAllConfigParamsTotalSize();

        Integer param1=(currentPage-1)*pageSize;
        Integer param2=pageSize;
        List<Params> paramsList= configurationVariablesMapper.queryAllConfigParams(param1,param2);
        if(paramsList!=null){
            r.setCode(200);
            r.setMsg("查询成功");
            jsonResult.setTotalSize(totalsize);
            resultList.add(paramsList);
            resultList.add(jsonResult);
            r.setData(resultList);
        }else if(paramsList==null){
            r.setCode(504);
            r.setMsg("数据库中无变量");
        }else{
            r.setCode(500);
            r.setMsg("服务器错误");
        }
        return r;
    }
    /**
     * 查询配置变量值的接口（用于下拉框显示）
     * @param param_id
     * @return
     */

    @Override
    public R queryParam_value(Integer param_id) {
        R r=new R();
        List<Params_Son> params_sonList=configurationVariablesMapper.queryParam_value(param_id);
        if (params_sonList!=null){
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(params_sonList);
        }else if (params_sonList==null){
            r.setCode(504);
            r.setMsg("改变量无变量值");
        }else {
            r.setCode(500);
            r.setMsg("后台服务错误");
        }
        return r;
    }
    /**
     * 添加配置变量值和变量id的接口
     * @param param_value
     * @return
     */
    @Override
    public R addParam_value(Integer param_id, String param_value) {
        R r=new R();
        int result=configurationVariablesMapper.addParam_value(param_id,param_value);
        if(result>0){
            r.setCode(200);
            r.setMsg("添加成功");
        }else {
            r.setCode(500);
            r.setMsg("该变量值已经存在");
        }
        return r;
    }
    /**
     * 下拉框中选中的配置变量值根据值的id进行删除
     * @param param_son_id
     * @return
     */
    @Override
    public R deleteParam_valueByParam_son_id(Integer param_son_id) {
        R r=new R();
        int result=configurationVariablesMapper.deleteParam_valueByParam_son_id(param_son_id);
        if (result>0){
            r.setCode(200);
            r.setMsg("删除成功");
        }else{
            r.setCode(500);
            r.setMsg("删除失败，服务器错误");
        }
        return r;
    }
    /**
     * 根据关键字模糊查询
     * @param paging
     */
    @Override
//    public R queryAllConfigByMessage(Paging paging) {
    public R queryAllConfigByMessage(Integer currentPage,Integer pageSize,String message) {
        JsonResult jsonResult=new JsonResult();
        List<Object> resultList=new ArrayList<>();
        R r=new R();
//        Integer totalsize=configurationVariablesMapper.queryTotalsize(paging.getMessage());
        Integer totalsize=configurationVariablesMapper.queryTotalsize(message);
        System.out.println(totalsize);


//        Integer currentPage=paging.getCurrentPage();
//        Integer pageSize=paging.getPageSize();


        Integer param1=(currentPage-1)*pageSize;
        Integer param2=pageSize;


//        List<Object> paramsList= configurationVariablesMapper.queryAllConfigByMessage(param1,param2,paging.getMessage());
        List<Object> paramsList= configurationVariablesMapper.queryAllConfigByMessage(param1,param2,message);


        if(paramsList!=null){
            r.setCode(200);
            r.setMsg("查询成功");
            jsonResult.setTotalSize(totalsize);
            resultList.add(paramsList);
            resultList.add(jsonResult);
            r.setData(resultList);
        }else if(paramsList==null){
            r.setCode(504);
            r.setMsg("数据库中无变量");
           jsonResult.setTotalSize(totalsize);
            r.setData(jsonResult);
        }else{
            r.setCode(500);
            r.setMsg("服务器错误");
        }
        return r;
    }


}
